#include "useradmindict.h"
#include "ui_useradmindict.h"
#include "useradminmodel.h"
#include "useradddialog.h"
#include <QMessageBox>
#include <QTextDocument>
#include <QSqlRecord>
#include <QSqlQuery>
#include <QSqlError>
#include <QSqlQueryModel>

UserAdminDict::UserAdminDict(QWidget *parent) :
    QDialog(parent),
    m_ui(new Ui::UserAdminDict)
{
    m_ui->setupUi(this);

    model = new UserAdminModel();
    m_ui->tvElements->setModel(model);
    model->setSelectionModel(m_ui->tvElements->selectionModel());
    m_ui->tvElements->setSelectionBehavior(QAbstractItemView::SelectRows);
    m_ui->tvElements->sortByColumn(1, Qt::AscendingOrder);

    connect(m_ui->tvElements, SIGNAL(doubleClicked(QModelIndex)),
            this, SLOT(on_pbEdit_clicked()) );

    m_ui->tvElements->setColumnWidth(0, 60);
    m_ui->tvElements->setColumnWidth(1, 150);
    m_ui->tvElements->setColumnWidth(2, 130);

}

UserAdminDict::~UserAdminDict()
{
    delete m_ui;
}

void UserAdminDict::changeEvent(QEvent *e)
{
    QDialog::changeEvent(e);
    switch (e->type()) {
    case QEvent::LanguageChange:
        m_ui->retranslateUi(this);
        break;
    default:
        break;
    }
}

void UserAdminDict::on_pbAdd_clicked()
{
    UserAddDialog userAddDialog(this);
    userAddDialog.exec();
    model->refresh();
}

void UserAdminDict::filtrate()
{
    model->filtrate(m_ui->leFilter->text());
    m_ui->tvElements->selectRow(0);
}

void UserAdminDict::on_pbEdit_clicked()
{
    int row = m_ui->tvElements->selectionModel()->selectedIndexes().at(0).row();
    UserAddDialog userAddDialog(this, model->record(row).value("id").toInt(),
                                model->record(row).value("name").toString(),
                                model->record(row).value("permission").toInt(), false);
    userAddDialog.exec();
    model->refresh();
}

void UserAdminDict::on_pbDelete_clicked()
{
    if (m_ui->tvElements->selectionModel()->selection().count() == 0)
        return;
    int row = m_ui->tvElements->selectionModel()->selectedIndexes().at(0).row();
    if( QMessageBox::question(this, tr("удаление записи"),
                              tr("Вы действительно хотите удалить пользователя <\b>\"%1\"<>?").arg(
                                      Qt::escape(model->record(row).value("name").toString())),
                              QMessageBox::Yes | QMessageBox::No, QMessageBox::No)  == QMessageBox::Yes)
    {
        QSqlQuery query;
        if(!query.exec(QString("DELETE FROM users WHERE id=%1")
            .arg(model->record(row).value("id").toString())))
            {
            QMessageBox::warning(0,"Невозможно удалить элемент!",
                                 "Невозможно удалить элемент по причине:" +query.lastError().text(),
                                 QMessageBox::Ok, QMessageBox::Ok);
        }
        else
            model->refresh();
    }
}

void UserAdminDict::on_pbEditPasswd_clicked()
{
    int row = m_ui->tvElements->selectionModel()->selectedIndexes().at(0).row();
    UserAddDialog userAddDialog(this, model->record(row).value("id").toInt(),
                                model->record(row).value("name").toString(),
                                model->record(row).value("permission").toInt(),  true);
    userAddDialog.exec();
    model->refresh();
}
